#include "namedpipe.hpp"

int main()
{
    My_Named_Pipe::Server ser("./fifo");
    ser._open();
    ser._read();
    ser._close();
    return 0;
}



int server()
{
    // 建立fifo文件
    umask(0);
    int fiforet = mkfifo(FIFO_PATHNAME, 0666);
    if (fiforet == -1)
    {
        ERROR_MESSAGE("mkfifo");
    }
    std::cout << "管道文件建立成功" << std::endl;

    // 打开文件
    int openret = open(FIFO_PATHNAME, O_RDONLY);
    if (openret == -1)
    {
        ERROR_MESSAGE("open");
    }
    std::cout << "读打开文件" << std::endl;
    std::cout << "开始读取->" << std::endl;

    // 读文件
    while (1)
    {
        char buffer[BUFF_SIZE] = {0};
        ssize_t readret = read(openret, buffer, sizeof(buffer));
        std::cout << buffer << std::endl;
        if (strcmp(buffer, "close") == 0)
        {
            break;
        }
    }

    // 关闭文件
    close(openret);
    std::cout << "关闭文件" << std::endl;
    sleep(1);

    // 删除文件
    unlink(FIFO_PATHNAME);
    std::cout << "删除文件" << std::endl;

    return 0;
}
